package cn.wolfcode.p2p.base.util;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by wolfcode on 2018/7/22.
 */
public class DateUtil {

	/**
	 * 添加天数
	 * 
	 * @param day
	 * @param days
	 * @return
	 */
	public static Date addDays(Date day, int days) {
		if (day == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(day);
		c.add(Calendar.DATE, days);
		return c.getTime();
	}

	/**
	 * 添加月数
	 * 
	 * @param day
	 * @param days
	 * @return
	 */
	public static Date addMonths(Date day, int month) {
		if (day == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(day);
		c.add(Calendar.MONTH, month);
		return c.getTime();
	}

	/**
	 * 获取当天的最后一秒的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getEndDate(Date date) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	/**
	 * 获取两个时间相隔的秒数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getTimeBetween(Date d1, Date d2) {
		return Math.abs((d1.getTime() - d2.getTime()) / 1000);
	}
}
